some re-factoring and code optimizations before unit testing

-work still in progress