Refactored reorderColumns handling code with a faster and more deterministic approach