Move counting and writing to /tmp into function.
Improve readability by using long flags.