Put main code in separate function, cleaned up comments