Copied algorithm from pickemup repo