Fixed up unit test and improved code