fixed edge cases and made more general, moved grid up.