DRYed up code. Fixed some tidbit bugs