improve error handling, add error codes