Added tests and fixed some obvious bugs