Added logging and error checking