Refactored window initialisation code for cocoa, making it more memory safe