Mostly completed version of the analysis script