Separated main input loop into small functions