finish refactor main func