Initial translation from C. (Doesn't compile yet.)
Fixed most compile errors.
Compiling, finally.