Tidy code + some optimisation