Refactored various bits of code.