applied code for algorithm use and fixed redundancy