Clean up duplicate code in loop handlers
Move test samples under test dir