Add generics to fix casting problem.
Make exception catching more concise.
Remove unused imports.
Merge branch 'refactor/generics' into refactor/convert-to-for-loops

These generics fix casting problems.