added logging support and some exception handling