added better exceptions and error checking