Fixed flawed logic in retrieving error code/msg
Working version