Refactor

Remove unused command handling
Avoid O(N^2)