massive refactor
add logging