fix tests, and clean up code