Added middleware setup and teardown