simplified code to generate views, added views and better commented subqueries