Speed up algorithm