minor refactoring
more refactoring to make the code more testable