Fixed and refactored the solver