Added custom error, but method doesn't work properly when running it multiple times