Added unit tests based on Wikipedia examples. Further fixes on the code based on unit tests.