Fixed tests, cleaned up code