Fix re-sorting not working correctly