Add locking and remove/fix some crufty code