Remove unused imports. Some code refactoring. Add very basic error handling