many bug fixes and tests