Code cleanup for error handling