Forked from Tobias Markus' extension