Refactor chunk Java code