Clean up the code some more. Closes #1051