refactor main, reduce duplicate code