Fixed major bug using window function