This section needs comments -- it's difficult to follow why this code is doing what it's doing.