Fix code and tests