separated out some code and truncated .each calls