Shorten function.. it's at the limit of 30 lines..