Some bug fix and adding new tests