made code more SML idiomatic