bug fixes and tests