Ensure middlewares only required once

Fixes #355