refactor out input loop