Really need to refactor all this into separate function or functions.