refactoring function main