improving algorithm