Defined functions and added comments. No functional change.