Finished java implementation