Made sort code more functional for readability.