I found the problem. The issue was that OldMouseX and Y were only assigned values once, and that time happened to be before the window had finished loading so they were always set to 0. The odds of you actually putting the mouse in the top left corner are pretty low, that's why it always closed.