Added some comments and reduced scope as was possible for function definitions.