Changed algorithm