refine and fix bugs