Initial bugfix with loops