added logging; improved error handling