refactoring main code