tidied solve function