added logging, fixed indent, refactored internals