Fixed #609
Fixed #552
Merged the changes to it