Refactoring, bugfixes, new commands, improved locking