Completely working version. Still can be cleaned up a little.