On further thought, this should probably be rewritten entirely.