Rough algorithm outline