Moved functions out of main()