add error codes, refactoring