modified database code to use forEach