No need to handle undefined case here
Extract function, improve error messages