Making more robust decisions internally

Cleaning code and breaking into functions

Making internal decision tree a bit more robust to errors and abscence of
commands