Code cleanup and a bit of refactor to address jsirois's review comments.