bug fixes and better accounting