Moved input handling to a seperate function