Merged with Matt's code