Extensive refactoring. Additional logging, amendments to using statements and scopes.