refactor master main() for easier reading