Changed flow of main code for readability