finished up algorithm