Refactored for unit testing