Adding threefold-repetition rule

- also tidied some things up :)