Fixed catching wrong Error
Refactoring
Added example test