Adds test cases to increase test coverage. Refactors code to enable adding tests by reducing the number of locations where `os.Exit()` is called from.