Adding some documentation and generalizing error handling.