Refactoring and fixed bug with input