Fix some code style and put main function at end of file