fix #122 #123 #131

Thanks to @MatthieuDartiailh!