major rewrite, mainly moved all calculations out of plots