Fix #610 and fix #540