Added comments and minor changes for clarity