Algorithm adaptation rewritten.