Added some tests and bug fixes