Fix a few warnings
Reviewed the code, corrected a few bugs, still needs to be tested