Moved window redefinition code into separate function