Fixing loop issue