Refactoring misc code