Main logic