Refactored my 2nd function to not use a RegExp, no bugs