adding reversed for-loop handling and cleanup