Changes to fix to latest trunk. Extra error handling.