Move some generic code to generics.js