Adding error conditions
Refactered to an better readable solution