several bug correction, refractoring