Refactoring and Cleanup

Added comments, streamlined code, making function names consistent, also
changed scope of utility functions to private.