refactor and attempt at simulated annealing