Cleaned up a bit of the file and reduced code duplication.