Changes to first algorithm