Simplified code and fixed tests.