improved logging
refactored internals for readability