apply some python magic to reduce the tabstops needed
add newline checks to avoid leaving trailing whitespace