Fixed some bugs described in Issue #3
Added more test cases based on Issue #3
Minor changes.