Move some functions into state.
Fix exists_in_column and exists_in_row
Add some unit tests