bug fix and simplification