Fixing N+1 query