Added comments and formatted for readability