(PUP-2568) Downcase class name before validating

Previously the check for a valid name was done against the raw input,
however it later downcased the name and used that form. This order caused
Class['A'] to be an error, which isn't desired (at least not until
PUP-1810 is taken care of). By just changing the order of operations
capital letters are once again allowed.

There is also a bunch of refactoring as I learned out the code worked. The fix
and test are in one commit and the refactorings in a different one to make
review easier.