ADD testcases and fix found bugs