Replicate the working code more closely