Fix some obvious bugs, need to write tests for this