Worked on checking end conditions