Removed spaghetti in input handling code.