Check for empty() before calling top().

Else it might crash sometimes.

Change-Id: I6a24fff83c3d36346debae5c0f2b8c0646a15c01