this works but not very well. need to get around for loop.
moved design matrix stuff into a separate function
renamed some variables.
replaced outer for loop with sapply... not sure if this speeds things.