Added additional logging, general refactoring.