bug fix: rewind file before counting number of lines

also only check number of lines once and then store it in variable