Clean up and document code