some improvements
(cherry picked from commit 00185be)