Fixed issue #361 and added test cases.