reducing code duplication and fixing unit tests