A copy of Kaashif's code but in Java