Some commenting, renaming of variables to have obvious meanings. Added test cases to unittests.