Logic in filter instead of looping many times