Re-arranged the solution for simplicity