refactor, reorganize code minimizing ambiguity, fixing whitespace