fixed bugs, restructured code some more