Organize and fix a few bugs