Improved implementation of Guy (1984).