Fix typos, deduplicate code, clean up