Bug when flattening: had mixed up argument order.
Minor code cleanup.  Separated x/y positioning into functions that can call instead of copy-pasting body of code between texts and rects.