added tabs, fixed some bugs, modularized code