improve regex, add tests for error handling