Fixed issues with OWIN setup.

See comments in the code to understand what was changed and why.