Really need to refactor this into separate function.