Fixed bugs found by statis analysis