Added tests and fixes