Big code refactor and demo2 improved