Revert "Revert "bug fixes.""

This reverts commit 1f834922c9b8e75ef697a5d2cf9319e55cc25a58.