restructured code to make testing easier