Merged first two code blocks because everything has to be perfect... except for the runtime efficiency, of course.