fixes a few bugs and organizes code differently