Reorganize and simplify code a bit.