Tidying up

Formalised the code more, made it more useful and organised.