Fixed MySQL problem with threading