refactored for unit testing