reorg, refactor common logic, add options, fixes