Refactor java code