Better algorithm