Updated and refactored for tests.