Code fixed and optimised