First working version. Not sure about bugs.