made code more testable and added tests