Main controller logic