cleaned up code. fixed bugs, still more cleanup to do