Fixed a few bugs. Still needs to verify