Initial middleware exploration
Some refinement of what middleware must do and some unit tests.