First draft

Working code that needs some adjustments.