Added a few nullchecks and also better logging