Cleaned up and refactored serial solution.