Found a shorter algorithm