Refactored and cleaned up various small bits of code.