added more logic to functions
solving mutliple for loop issues.
updated comments