Large rewrite of input handling