complex code refactoring