Implement the middleware specification

Took some thinking but I finally cracked it. The problem is that
a middleware may trigger itself again unless explicitly disabled. I
ended up using a state flag for this purpose set on first round. That
way middleware won't be called for any subsequent calls of the model
itself.