Added some error checking which will help with buggy function calls