Updated. Using a buffer instead of writing directly to stdout (it was causing some blink problems in certain terminals).