Improved outline handling

Should solve overflow problem in most cases.

Update: fixed @astiob's issues.