Copied algorithm from pickemup