Refactoring chaining of functions with support for generics