This is a pretty good working version. No need to shorted the code further.
Let's write analysis functions.