Calculating summary statistics
Final version, including output to database