Refactor window function