some minor bug fix and optimization