50+ lines, let's break this up into a couple of smaller functions.