shouldn't you do hash.endsWith("\n") to only target trailing newline?