Refactor the long #valid? method.