Clean up window listener code and make sure it works on OS X