Cleaned up the code, and updated tests