Wrote some tests. fixed a few bits.