Refactored.
Takes own previous state into account for next state.
Refactors everything into separate code files.