First draft of final solve function