Ported changes to auth processing
Merge pull request #3 from loz/master

Extracts Auth processing logic