Fixed bugs in the current implementation. Also added some new tests