Added support for own error codes.
Unit tests and refactoring.