Finally got a working version.