refactored code which is concern with key input