Cleaned up code and move common patterns to functions like OnWindow for all access to _window and WithMessageLock.