implemented algorithm A (untested)