My re-factoring of Mike's code.