Working code, needs commenting.