Refactor main function; Log fatal errors in a single place