bug fix and refact