Finalise algorithm