Overhauled code

No more spaghetti code