Added Thread-safeness to functions and thread stuff