Removed a lot of code duplication and redundant #includes.