Revised logic to more closely follow paper