add validate, refactor validate a bit, use more pattern matching.