Recoded a lot of this.