Removed session variables, added better error handling