remove argparse debugging code
add check_filehandle() for future filename/handle agnostic behaviour
adjust code structure so it can be imported in python scripts

This is done by moving all worker-code from the main() into a separate function
(called gen_stats()) that is called either from the main or from the code that
imported this module. The main() is now only responsible for the argument
parsing part (and calling the worker, of course).
minor rearrangements