added logging and error checking