refactored context's writer to be a bit safer
simple middleware implementation