Refactor main code