refactor the java code