fixed loops to improve speed of STAN model