Added update function and optimised code.