Started porting some of Andrew Main's C code