This appears to work, but I'm certain there's a more elegant solution
A smidge of cleanup