Minor code cleanup

Replace nested if-then-else and progn-blocks with a cond (which has the
same control flow and implicit progns)
Make dirty-bit tracking more reliable