Refactored to a first working version.