Refactored validation by splitting into functions. Fixed a bug relating to the state representation.