Refactored version with handle some exceptions