Added error handling and additional whitespace tolerance.