shoudl this be if (valid) rather than if (!valid) ?