upgrade type functions, add sorting