First working version of loopmodel code