fix #502. fix #494. Added test for issue #494