cleanup of code and adding tests