Fix and improve deriving for indexed data types