first draft of complete solution