Refactored bubble sort