added logging and error catching