bug fixes and tighter checks