Added tests and fixed some bugs. #186