bug fix and refinement