codes clean up and some bug fix