refactor main code