Optimise code and improve functionality