Some optimization and improved error messages.