Refactor Java code