Finalised use of substring and worked on optimisation.