Major Refactor
change flags to states
Refactor tests