Something close to a working version, still with some serious bugs :(