Improving algorithm