Main logic

Now it's working.