Removed unused code, fixed undefined variables