rewrote Java version to more Java way