By Benjamin. Fixed bug 2723 and 2731.