refactor
more error checking