Added tests and fixed some bugs while writing tests.