fix for #158

@settinghead please review. This is just the fix for exception. The main fix for this issue is in the Core API.