Reducing more complexity

Separating out duplicate code in filter and order functions so it can
be reused.