Some code improvements

Created an enumeration to control returned error ID.
Some code tidying.