bug fixes and removed most debugging fprint statements (for performance reasons)