Fixed edge cases on sorting alg