Restructure organization of code
Fixed a few bugs