Refactored errors handling