Added better logging, slight refactoring.