using forEach to remove copy-paste code