cleaned up code, added more unit tests