bug fix and tests improved