refactored code, simplified keyboard handling, js-lint-ed