Refactored code according to suggestions by Dima Vidmich (http://dou.ua/forums/topic/10298/#499448) to expand threads