Moved minimum variance calculation into separate function