Massive refactor - can't remember everything I've done.