Replace slow Ruby implementation with D