main function moved to the top.