Your code would be cleaner if you could use another function instead of writeln(). One that handles new-line characters. In that case, you can get rid of all the extra writeln() calls for your empty lines. Not sure if such a function exists though.