Add argparse and logging; various efficiency improvements & bugfixes