Huge code clean up and added comments