unlock if not empty

The one important change here is that it unlocks the semaphore if the received frame is not empty, not if it is empty. The rest are minor cleanups.