Preparations to make it work at the new location + minor comment formatting edits
Make it work at the new, temporary location + factor the if block at the end
Make it work at the new, temporary location and replaced the weird formula in the SQL with a more idiomatic equivalent