Added speed comparison with naive method