[IMP] re-factored code, misc changes