Add support for the optional 'resname' attribute.
Code cleanup and refactoring.