replaced two database call with one and a for loop