To catch twitching, you should slightly tap A or D. How to fix it? You need to round the current angle of the cube to an angle that is divisible by 90 without remainder. "angle MOD 90" or something like this.
alright, I'll add a fix to that after the jam. Thanks for elaborating!