Changed lock system, fixing (hopefully) all problems

This is INSANELY annoying and complicated, so please do not mess this
up. Thank you.