optimized keyboard controller - first draft; some refactoring