Fixed several bugs which could potentially cause server crashes.