Bug fixed, code tidied, some optimisation