fixed recursive function to not be recursive