some improvements to the algorithm, finished implementation with images.