Completely rewritten debouncing routine

Rewrote all debouncing related code for improved accuracy.