Refactoring of function types. Also fixes some library import bugs.