Moved order condition into proc as it can't be evaluated too early or DB access occurs.