Several bug fixes
Added more tests