Refactor implementation; Unit tests

Can you please test this with your proxy server setup and let me know if everything is working?  
Please also let me know about any usage difficulties.  

If it looks good, please accept this pull request.  I hope it will update on your PR to UA/java-library.  I will probably get another set of eyes on this before final approval.