fix #3072 and some cleanups.