refactor main entrypoint
tidy up test_main and add flag test
backdoor tests