Complete rewrite

It wasn't working so I rewrote it