Rewrote as class

Needs testing and probably fixing