another attempt to fix crash issue due to failure to properly detect end conditions