Refactored keyboard control and tested
Additional refactor