Added tests and bug fixes