Working algorithm implementation.