First working version, more code clean up required.