Ported functionality from Lex/nomen-re