improved modularization of main function