updated sort method, cleaned code