Tweaked regexes and added more error handling