cleaned up formatting, moved functionality to its own function (bah, tabs!)