This is the refactored version. Note that I don't make use of select
statements.