Refactoring this later; reverting a brief attempt.