Refactored selection sort