Apply safeware patterns for middleware.

Rationale: https://gist.github.com/raggi/4463147