Clean up hyperparam optimization code significantly and prepare to add other functions