Less hacky version of previous patch