changed variables and consolidated the function