Adding better logging and some refactors