Functions code reformatting

Big tidy up! Improve comments and re-write and simplify documentation.