Made major logical changes