improve code to handle scalar types