flatten any middleware and bind every function to method context