Improved processing of the cookie from the middleware

I'm not a Linq ninja so there could be a cleaner refactoring.
Also, it's my very first github fork/commit/pull so excuse any mistake in the process! :)