Fixing up code and test cases