Rewriting the count_lines function

Faster and easier to read and understand, and higher performance.