Simplify code using generators