typos
optimization and logging
refactoring - but not finished