Rearrange functions and simplify whitespace handling