added logging and cleaned up a bit of code