Partial refactor, removed unnessary variables
More aggressive simplification of solution