optimize and fix some bug