Removed unused imports. Some performance related changes to loops etc.