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