Revamp to document, follow new conventions, and add debugging log statements.