move input processing loop into a function (replace 5 occurences with call to function)