Working. Needs a little testing and commenting.