Added appropriate locking to main TeaSafe code