fixed the algorithm to be checkstyle compliant