Added error code detection and logging