Refactor filter writer code