Updated to be the clean C code that was written for Emma