Updated code to use field API and add more inline comments