Maybe the most complete version of this yet