Fixed and added better error handling