Fixed issues with loops and added comments