This explains how we come up with great ideas while sleeping. It happened to me sometimes waking up in the middle of the night saying: "Yes! That's it!" because of a problem with no solution bugging me the day before.