Added type cast for order variable. Added check for array of templates before foreach.