Improved command handling

Removed unused imports, added type checking for every command.