Added more unit tests and fixed some of the related code