Changed to use functions

Changed code to use functions instead of being all in main.