Hack to avoid race condition. Courtesy of WGH from Habrahabr. Should fix bug #5.