new algorithm, java version