do you know any better way to rewrite this code? it originally used lists and flatten even though option would suffice.