Changed algorithm to use a switch statement

makes it stable.
working on figuring a way to do it dynamically for cases outside the
grid