Removed unused variables and fixed unhandled exception