Refactoring for handling resource management