Finished rewriting API with support for pluggable middlewares