Cleaned up code to generate proper principals