Stricter mutex protection, fix stdio errors and compiler warnings