Added code to properly destroy window