Rewrite code for snake logic