split casting code out of command loop; modified command loop parsing