add do_filter func to reduce duplicated code

there are some codes can be replaced by introducing a do_filter function.