refactor a bit

- Add comments.
- Move function writeAProduct.