Handle multiple spaces
Fix an edge case whith newlines
Refactor complex if condition

That mess was hard to read and harder to understand.