Expand functions slightly
Add more safety checking