added truncate support, code cleaning
fix some scope issues