Fixed issue #361 and added test cases.
Merge pull request #365 from YifanRen/master

Fixed issue #361 and added test cases.