Fix #662 and #667

Fix #662