Fix code and unit tests