Rewrite pretty-printer to be more robust and faster