Simplify the code by doing the filtering earlier, rather than later