Fixed remaining code, tested and working on MacOS