TODO: make a function out of this.