fixed edge case at end
fixed some shadowing issues