Fixed bug with threading, causing memmory violations