I can't help you just from looking at the code. Please specify which line is giving you the crash. You can place a beep() at the beginning of the function. If you hear the beep before the crash place it one line further down until the crash happens before the beep().
Always learn from history, to be sure you make the same mistakes again...