Bugfixes

Bugfixes and increased error handling.