Fixed few thread-safety problems