Refactored processCommand method

…and added more comments and documentation