reordered main function