added behaviour coding and did some refactoring