added new version with some changes to the variables and also conversion of loops. problem still not resolved though