Moved command processing into a separate function