added some comments and expanded some code for readability