fixed logic, passed tests [MJ-391]