Refactored errorhandling