Moved main code to a main function