rewrited. next step: recoding in java