porting #44 and fix #48