Add support for pluggable middlewares. fixes #16, #12, #11, #5, #1