Some bug fixes and tests added