First attempt at solve() function - horribly slow