bug fix and some minor improvements.