Fixed a minor bug, reorganized and added more tests.