added propper memory freeing to sort_tables. insanely smaller memory footprint