Fix bugs and clean the code