refatored the valid? method