added more safty to locking routines