Refactor code and tweak style.