Main logic

Now it's working.
![43099ewjare](https://cloud.githubusercontent.com/assets/8445923/4085400/51bc554e-2f1b-11e4-906d-b6a3276dd7c3.jpg)