new method for sorting and cleanup