Fix various syntax and runtime errors