Main logic changed and implemented