fix bugs and clean the code