Improved errorhandling

Improved errorhandling