More comments, refactored, added some tests.