Refactor template for latest sorting code