added locks for thread safety