I found out there was a standard method for doing what I had hacked out...