Much simpler implementation, thanks to kachitta