still improving algorithm

semi-stable version