... a key feature of deep NREM sleep, which predominates early in the night, is to do the work of weeding out and removing unnecessary neural connections. In contrast, the dreaming stage of REM sleep p, which prevails later in the night, plays a role in strengthening those connections.

from the book Why We Sleep: The New Science of Sleep and Dreams by Matt Walker.