Several bugs fixes