refactored code out of Main