Cleanup source code of unused imports and missing annotations (also fixes #365)

Also I've tried to remove as many NPE points as I've could and wrap the ones that I couldn't in a try / catch.