Revised Algorithm