bug fix and code cleaning