Restrucuralised code to make it more sensible (      removed lines :(      )