optimize algorithm