Error handling and minor cleanup.