improved error checking and logging
merge