Reverting to ezust's version. This is a tricky problem.
Added log message
Modified to run outside of the event dispatch thread.