fix #79 and part of #39