Sorted and grouped the records based on realname.
Cleaned up the code