First attempt at modified algorithm