Added majority of code