main refactor