Fix breaking tests, and undeclared 'ended' variable
Tidy test