Add chainability and middleware function and tests