Fix and optimize element rice code