A slightly less hacky version