Improved implementation of e7344d88.