Refactored sorter.php