Slightly restructured the code.