Make validate safe for serialization
Improve error message
Seclude `labels` definition to outer module