Code somewhat cleaner; added functions.