BUG: sorting logic