bug corrections and optimisation