Added mutexes and some bugfixes