New code in preparation of LDA
tidied code