Fixed exact algorithm (maybe?)