Separate some utility functions so that better error handling can be provided.