Clean up sorting routine