Fixed error handling within functions