Didn't update my original algorithm, here is the up to date one