Move counting reads to functions.
Fix paths so temp files are in temp directory.