added more error checking and better messages