Add error codes for reference.
Move logging into seperate function.