Main while loop cleaned up