Added some error checking routines and moved error handling code to new files.