ticket:3 middleware refactoring

Use dict.get() instead of self-written bicycle; PEP8 fixes