This is a pretty large hunk of code; I'd rather split it out into its own file.