Reorganize main() function