Major rewrite after an epic faily of copy and paste