Code reorg: lets migrate functionality out of Main