Code Optimisation

Speed improvements by Carlos-SZ