OSX: Beginner Question: problem w/ Screen, Flip

Hi all,
I am new to Matlab and Psychtoolbox, though I do have experience programming. I am
trying to learn Matlab & PTB in order to run some pretty straightforward memory
experiments using paired associates. For example, presenting "Arch = Sandal" and then
later showing "Arch = ???" and requiring the participant to respond with an answer.

I'm working under Mac OS X.

Right now I'm just trying to figure out how displaying things works. The whole Screen
system is foreign to me, and I've been unable to gain an understanding of it through the
help functions or through anything I've found online. Here's a specific problem I'm having:

I'm working with the basic "Hello World" demo provided with PTB, and have added a short
matrix of word pairs which are displayed in a for loop. Everything looks fine, until I hit a
key after the 'Hit any key to exit.' appears. Then, the last word pair that was shown ("arch
= sandal") is shown again briefly, apparently while the window is being closed, or
something. I'll paste my code below.

I think this might have something to do with Screen('Flip'). I'm not clear on what
exactly that function does or why, but I know that it's somehow necessary. Thanks very
much for any assistance,
Jason


wordpairs = {...
'comment' 'hint';...
'cluster' 'necklace';...
'kiln' 'hammer';...
'custom' 'tomb';...
'rim' 'granite';...
'arch' 'sandal'}

try
screens=Screen('Screens');
screenNumber=max(screens);
w=Screen('OpenWindow', screenNumber,0,[],32,2);
Screen('FillRect', w, [0, 0, 0, 255]);
Screen('TextFont',w, 'Courier');
Screen('TextSize',w, 50);
Screen('TextStyle', w, 0);

for loopCond = 1:size(wordpairs,1)
Screen('DrawText', w, [wordpairs{loopCond,1} ' = ' wordpairs{loopCond,2}], 100, 100,
[0, 0, 255, 255]);
Screen('Flip',w);
WaitSecs(1);
end

Screen('DrawText', w, 'Hello World!', 100, 100, [0, 0, 255, 255]);
Screen('TextFont',w, 'Times');
Screen('TextSize',w, 30);
Screen('DrawText', w, 'Hit any key to exit.', 100, 300, [255, 0, 0, 255]);
Screen('Flip',w);
KbWait;
Screen('CloseAll');
catch
Screen('CloseAll');
rethrow(lasterror);
end