Improved logging and error checking