fixed some bugs, seems to work in a testing enviornment