Additions to second algorithm