added some logic for mlock (buggy)