added assert and changed optimization