Fix compiler warnings and potential crash