Added, total and avg filtering.
Refactored filter code.