First working version. Cleaned up a bit