Fix a bug where the limit variable was repeatedly overwritten during a loop due to the recent refactoring work.
Add extra debug log information about the atoms and sizes being written out.