refactoring codes, handle Kind more generally