cleaned up code and tried to fix tests