Code not reliable - any way to 'foolproof'?

Thank you! There is also an issue with repeater item spacing (between first and second item for example) - don’t know if this is related?