better implementation from his @coderanger-ness