Cleaned up code, added new tests.