added logging, refactor a few things