restructured code to be more testable