Fix issues #493, #502 and #509

I would like to resolve #493, #502 and #509. Thanks.