If you limit the variable to -16 it could work, I didnt read the code fully...
Just a simple idea