bug fixes and add tests