Improved message/code error handling

Change-Id: Icbef71a5d29d9086d7b086d1b25dcc062a6efc36