Copied Matt's code to a separate file.