added better dependency and error checking