added better function flow