finallize function