This is the code I use in my setup() for initializing the SD card
That's less than 1/3 of the code you need to post.