added logging and error handling