Added comments to describe partial functions.
Moved item and log queries outside of data_type loop to limit queries.