restructure the code to add main function.