Refactoring having clause (not finished)