Refactor of keypress listener