First working version. Pretty incomplete.