juggle the order of variable initialisation around. remove unused variable and convert some ifs to one-liners