Algorithm implementation code