improve error path, and minor adjustments.