Refactor base URL retrieval and add unit tests.
Fix formatting.