fixed locks and memleak