clarify types; add more error checking