fix bugs and add main function