extract main function