Refactor slightly, add comments