refactored reorder logic