Fix tests and add comments to all functions