Add multithreading and optimisation