optimaize code for filter