added proper accounting, some new bugs expected