changed method of optimizing