Cleaned up a bit. Moved out the cd stuff into a subfunction.