Refactor calculation into separate function