updated the code and fix bugs