I think the issue is with the normalize() function right above where you check the length. This is because it will make the length 1 regardless of the magnitude, so a very small number that is close to zero (but not zero) will become 1. I think you want to remove the normalize function, then below check if the length is greater than speed, that should be correct. Then you might need to normalize inside the if statement before your other calculations.