Optional: this whole block should be moved into a helper function with a descriptive name.