tidied some error handling
tidier _setmetadata() function