Add comments and refactor a bit.